import requests
from parsel import Selector
import json


class ShopDetail:

    def __init__(self, url):
        self.url = url
        self.shopTabs_url = "https://www.dianping.com/ajax/json/shopDynamic/shopTabs"
        self.reviewAndStar_url = "https://www.dianping.com/ajax/json/shopDynamic/reviewAndStar"
        self.params = {
            "shopId": "",
            "cityId": 344,
        }
        self.cookies = {
            '_lx_utm': 'utm_source%3Dbing%26utm_medium%3Dorganic',
            '_lxsdk_cuid': '182117ddf1fc8-0174e6028847dd-4c647e53-384000-182117ddf1fc8',
            '_lxsdk': '182117ddf1fc8-0174e6028847dd-4c647e53-384000-182117ddf1fc8',
            'WEBDFPID': '7x93317yu81w5vz0054wwy257y03y0318107z1z73xy97958702wx99w-2003995845181-1688635843949KIKGKAEfd79fef3d01d5e9aadc18ccd4d0c95073644',
            '_hc.v': '1cde0cce-614f-4662-ba6d-60cd73fead9b.1708049442',
            'qruuid': '71ae4647-4e1d-4574-9167-dd71e2cb17b6',
            'dper': '0202c9258c9828f11698a95f036519d5a08e9077cfb20c7b4f2dc8c79b5f86344a6d2138b43d20dade0d8beba3c9dab080d62914cdbe35a9c86300000000ff1d00007e8b778087fe8b54ef676252daf73bc1128e374c89433a8c026d8d80eb05c8e2de873770f8cff79e5737aa6225ebfe1e',
            'fspop': 'test',
            'cy': '344',
            'cye': 'changsha',
            's_ViewType': '10',
            'll': '7fd06e815b796be3df069dec7836c3df',
            'Hm_lvt_602b80cf8079ae6591966cc70a3940e7': '1708049478,1708565701,1708574157',
            'Hm_lpvt_602b80cf8079ae6591966cc70a3940e7': '1708577879',
            '_lxsdk_s': '18dcf2d648a-f75-3e4-b7b%7C%7C43',
        }
        self.headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'Cache-Control': 'no-cache',
            'Connection': 'keep-alive',
            'Pragma': 'no-cache',
            'Referer': 'https://www.dianping.com/changsha/ch10',
            'Sec-Fetch-Dest': 'document',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': 'same-origin',
            'Sec-Fetch-User': '?1',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0',
            'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
        }
        self.html = ""
        self.shopTabs_data = {}
        self.reviewAndStar_data = {}
        self.shop_detail = {}

    def get_static_data(self):
        """
        静态数据： 店铺名字、评价总数、人均价格、口味评分、环境评分、服务评分、店铺地址、营业时间
        """
        try:
            response = requests.get(self.url, headers=self.headers, cookies=self.cookies)
            response.encoding = response.apparent_encoding
            self.html = response.text
        except:
            pass

    def parse_static_data(self):
        selector = Selector(text=self.html)

        self.shop_detail = {
            "shop_name": selector.xpath('//h1[@class="shop-name"]/text()').get(),
            "review_count": selector.xpath('//span[@id="reviewCount"]/text()').get(),
            "avg_price": selector.xpath('//span[@id="avgPriceTitle"]/text()').get(),
            "taste_score": selector.xpath('//span[@id="comment_score"]/span[1]/text()').get(),
            "environment_score": selector.xpath('//span[@id="comment_score"]/span[2]/text()').get(),
            "service_score": selector.xpath('//span[@id="comment_score"]/span[3]/text()').get(),
            "address": selector.xpath('//span[@id="address"]/text()').get(),
            # "business_hours": selector.xpath('//div[contains(@class, "other")]/p[contains(@class, "info")]/span[@class="item"]/text()').get(),
            "business_hours": selector.css('div.other p.info span.item::text').get(),
        }

    def get_dynamic_data(self):
        """
        动态数据：综合评分、推荐菜
        """
        try:
            self.params['shopId'] = self.url.split('/')[-1]
            self.shop_detail['shopId'] = self.url.split('/')[-1]
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0',
            }
            response_shopTabs = requests.get(self.shopTabs_url, headers=headers, cookies=self.cookies, params=self.params)
            self.shopTabs_data = response_shopTabs.json()
        except:
            pass

        self.params['mainCategoryId'] = 1
        response_reviewAndStar = requests.get(self.reviewAndStar_url, headers=headers, cookies=self.cookies, params=self.params)
        print(response_reviewAndStar.text)
        self.reviewAndStar_data = response_reviewAndStar.json()

    def parse_dynamic_data(self):
        self.shop_detail['recommend_dishes'] = self.shopTabs_data['priceMapForDishesWithPic']
        self.shop_detail['five_score'] = self.reviewAndStar_data['fiveScore']

    def save_data(self):
        print(self.shop_detail['shop_name'])
        with open('../data/shop.json', 'a', encoding='utf-8') as f:
            json.dump(self.shop_detail, f, ensure_ascii=False, indent=4)
            f.write(',\n')

    def run(self):
        self.get_static_data()
        self.parse_static_data()
        self.get_dynamic_data()
        self.parse_dynamic_data()
        self.save_data()

def main(url):
    spider = ShopDetail(url)
    spider.run()


if __name__ == '__main__':
    url = "https://www.dianping.com/shop/k8dKnrQg5ytBwxtC"
    spider = ShopDetail(url)
    spider.run()

